const Router = require('koa-router');
const router = new Router();
const {db, QueryTypes} = require("../../models/db");
// 保存送货路线区域
router.post('/deliver/grpOrders', async ctx => {
    let ids  = ctx.request.body.ids;
    let tenantId = ctx.user.tenantId
    ids = ids.join(',');
    let sql = `select invName,spec,unit1,unit2,unit1 unit,round(qty/spec,4) qty,
       case when mqty=0 then concat(sqty,unit2) when sqty=0 then concat(mqty,unit1) else concat(mqty,unit1,sqty,unit2) end assQty from
    (
        SELECT invName,spec,unit1,unit2,cast(qty/spec as int) mqty,cast(qty%spec as int) sqty,qty from
        (
            SELECT ii.invName,ii.spec,ii.unit1,ii.unit2,sum(ii.qty*ii.calNum ) qty from 
           invoice iv inner join  v_main_info ii on iv.tenantId=ii.tenantId and iv.id=ii.iid and iv.tenantId=${tenantId} and iv.id in (${ids}) 
            where iv.transType in(150601,150603) and ISNULL(ii.trans, '')<>N'换入' GROUP BY ii.invName,ii.spec,ii.unit1,ii.unit2 
        ) a
    ) a`
    let ret = await db.query(sql, {type: QueryTypes.SELECT});

    return ctx.suc('', ret);
})




module.exports = router;
